package cn.zjitc.quickstart.controller;

import cn.zjitc.quickstart.domain.User;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller
public class DemoController {
    @RequestMapping("/index")
    public String index() {
        return "index";
    }
    @RequestMapping("/hello")
    public void hello(){
        System.out.println("hello");
    }
    @RequestMapping("/quick2")
    public ModelAndView quick2(){
        ModelAndView mv = new ModelAndView();
        mv.addObject("username","zjitc");
        mv.setViewName("success");
        return mv;
    }
    @RequestMapping("/quick3")
    public String quick3(Model model){
        model.addAttribute("username","就很烦");
        return "success";

    }
    @RequestMapping("/quick4")
    public String quick4(HttpServletRequest request){
        request.setAttribute("username","zjitc");
        return "success";
    }
    @RequestMapping("/quick5")
    @ResponseBody
    public String quick5(){
        return "hello Spring MVC";
    }
    @RequestMapping("/quick6")
    public void quick6(HttpServletResponse response) throws IOException {
        response.getWriter().write("hello Spring MVC");
    }
    @RequestMapping("/quick7")
    @ResponseBody
    public String quick7(){
        return "{\"username\":\"zhangsan\",\"age\":18}";
    }
    @RequestMapping("/quick8")
    @ResponseBody
    public String quick8() throws JsonProcessingException {
        User user = new User();
        user.setName("zhangsan");
        user.setAge(18);
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(user);
    }
    @RequestMapping("/quick9")
    @ResponseBody
    public User quick9(){
        User user = new User();
        user.setName("zhangsan");
        user.setAge(18);
        return user;
    }


}